package com.nb.controller.doctor;


import com.nb.result.Result;
import com.nb.service.DoctorService;
import com.nb.vo.DoctorVO.*;
import com.nb.vo.DoctorVO.DoctorBodyOV.*;
import io.swagger.annotations.ApiOperation;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/doctor")
public class DoctorController {

    @Autowired
    private DoctorService doctorService;


    @GetMapping("/getInfo")
    public Result getDoctorInfo(HttpServletRequest request) {

        DoctorGetInfoVO doctorInfo = doctorService.getDoctorById();

        return doctorInfo == null ? Result.error("查找失败") : Result.success(doctorInfo);
    }

    @PatchMapping("/modifyInfo")
    public Result updateDoctor(@RequestBody DoctorModifyInfoVO doctor) {

        return doctorService.updateDoctorById(doctor) ? Result.success() : Result.error("更新失败");
    }

    @PostMapping("/publishSource")
    public Result doctorPublishSource(@RequestBody DoctorPublishSourceBodyOV ov) {

        return doctorService.DoctorPublishSource(ov) ? Result.success() : Result.error("号源发布失败");

    }

    @GetMapping("/getPatientList")
    public Result doctorGetPatientList(HttpServletRequest request) {

        List<DoctorGetPatientList> patientLists = doctorService.doctorGetPatientListById();


        return Result.success(patientLists);
    }

    @GetMapping("/getConductedPatientList")
    public Result doctorGetConductedPatientList(HttpServletRequest request) {

        List<DoctorGetConductedPatientList> conductedpatientLists = doctorService.doctorGetConductedPatientList();


        return Result.success(conductedpatientLists);
    }

    @GetMapping("/getOrderList")
    public Result doctorGetOrderList(HttpServletRequest request) {

        List<DoctorGetOrderList> doctorGetOrderList = doctorService.doctorGetOrderList();

        return Result.success(doctorGetOrderList);
    }

    @GetMapping("/getConductForm")
    public Result doctorGetConductForm(@RequestParam String id) {

        DoctorGetConductForm forms = doctorService.doctorGetConductFormById(id);

        if (forms == null) {
            String msg = "病人暂时没有表单数据！";
            return Result.success(msg, forms);
        } else return Result.success(forms);
    }

    @PatchMapping("/submitDiagnosis")
    public Result doctorSubmitDiagnosis(@RequestBody DoctorSubmitDiagnosis ov) {

        return doctorService.doctorSubmitDiagnosis(ov) ? Result.success() : Result.error("提交诊断失败");

    }


    @PatchMapping("/submitPrescription")
    public Result doctorSubmitPrescription(@RequestBody DoctorSubmitPrescription ov) {

        return doctorService.doctorSubmitPrescription(ov) ? Result.success() : Result.error("提交医嘱失败");

    }

    @PostMapping("/makePrescription")
    public Result doctormakePrescription(@RequestBody DoctorMakePrescription ov) {

        return doctorService.doctorMakePrescription(ov) ? Result.success() : Result.error("提交处方失败");

    }

    @PostMapping("/checkApply")
    public Result doctorCheckApply(@RequestBody DoctorCheckApply ov) {

        return doctorService.doctorCheckApply(ov) ? Result.success() : Result.error("提交检查检验申请失败");

    }

    @PatchMapping("/completeConduct")
    public Result doctorCompleteConduct(@RequestBody DoctorCompleteConduct ov) {

        return doctorService.doctorCompleteConduct(ov) ? Result.success() : Result.error("完成接诊接口失败");

    }

    @GetMapping("/getPrescriptionList")
    public Result doctorGetPrescriptionList(HttpServletRequest request) {

        List<DoctorGetMedicalAdviceList> medicalAdviceList = doctorService.doctorGetPrescriptionListById();
        return Result.success(medicalAdviceList);
    }

    @PatchMapping("/modifyPrescription")
    public Result doctorModifyPrescription(@RequestBody DoctorModifyMedicalAdvice ov) {

        return doctorService.doctorModifyModifyMedicalAdvice(ov) ? Result.success() : Result.error("无权修改此医嘱");

    }

    @GetMapping("/getSourceList")
    public Result doctorgetSourceList(HttpServletRequest request) {

        List<DoctorGetSourceList> forms = doctorService.doctorGetSourceListById();

        if (forms.isEmpty() || forms == null) {
            String msg = "此医生暂时没有未来7天的号源表单数据！";
            return Result.success(msg, forms);
        } else return Result.success(forms);
    }

    /**
     * 更换医生头像
     *
     * @param file 头像文件
     * @return 更换结果，包含新头像的完整 URL
     */
    @PutMapping("/changeAvatar")
    @ApiOperation("更换医生头像") // 添加 Swagger 注解
    public Result<String> changeAvatar(@RequestParam("file") MultipartFile file) {
        log.info("更换医生头像");
        if (file.isEmpty()) {
            return Result.error("上传文件不能为空");
        }
        // doctorService.changeAvatar(file) 返回的是完整的 URL
        String avatarPath = doctorService.changeAvatar(file);
        // 将完整的 URL 作为数据返回给前端
        return Result.success("头像更换成功", avatarPath);
    }




    }
